package com.atwork.wuhua.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.base.SystemBean;
import com.atwork.wuhua.mvp.presenter.SplashPersenter;
import com.atwork.wuhua.mvp.view.ISplashActivity;
import com.atwork.wuhua.utils.GlideUtils.GlideUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import pub.devrel.easypermissions.EasyPermissions;

public class SplashActivity extends BaseActivity implements ISplashActivity {
    @BindView(R.id.img_splash)
    ImageView imgSplash;
    private int sysSdkVersion = Build.VERSION.SDK_INT;
    private static final String[] LOCATION_AND_CONTACTS =
            {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE};
    private static final int RC_LOCATION_CONTACTS_PERM = 124;

    private SplashPersenter persenter;

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        persenter = new SplashPersenter();
        persenter.attachView(this);
        if (sysSdkVersion < Build.VERSION_CODES.M) {
            startMain();
        } else {
            locationAndContactsTask(); //Android6.0申请权限
        }
    }

    @SuppressLint("CheckResult")
    private void startMain() {
        persenter.getRedirect();
    }

    @SuppressLint("CheckResult")
    @Override
    public void intentType(final SystemBean bean) {
        if (bean.getData().getState().equals("2")) {
            Observable.timer(1, TimeUnit.SECONDS)
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                            Bundle bundle = getIntent().getExtras();
                            if (bundle != null) {
                                intent.putExtras(bundle);
                            }
                            startActivity(intent);
                            finish();
                        }
                    });
        } else {
            Observable.timer(1, TimeUnit.SECONDS)
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            Intent intent = new Intent(SplashActivity.this, WebViewActivity.class);
                            intent.putExtra(ConstantsMyself.INTENTURL, bean.getData().getLink());
                            startActivity(intent);
                            finish();
                        }
                    });
        }

    }

    public void locationAndContactsTask() {
        if (hasLocationAndContactsPermissions()) {
            // Have permissions, do the thing!
            startMain();
        } else {
            // Ask for both permissions
            EasyPermissions.requestPermissions(
                    this,
                    getString(R.string.rationale),
                    RC_LOCATION_CONTACTS_PERM,
                    LOCATION_AND_CONTACTS);
        }
    }

    private boolean hasLocationAndContactsPermissions() {
        return EasyPermissions.hasPermissions(this, LOCATION_AND_CONTACTS);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case RC_LOCATION_CONTACTS_PERM: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startMain();

                } else {
                }
                return;
            }
        }
    }

}
